import functools

from flask import current_app
from flask import g
from flask import session


def do_index_class(index):
    """自定义过滤器,过滤点击排序html的class"""

    if index == 0:
        return "first"

    elif index == 1:
        return "second"

    elif index == 2:
        return "third"

    else:
        return ""


# 登录验证的闭包
def verify_login(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        # 默认值
        user = None
        if user_id:
            try:
                # 根据id查询当前用户
                from info.models import User

                user = User.query.get(user_id)

            except Exception as a:
                # 记录日志
                current_app.logger.error(a)
        g.user = user
        return func(*args, **kwargs)
    return wrapper
